<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class FinanceAgentDiamond extends Base
{
    protected $table = 'agent_diamond_log';
    protected $diamondStreamTable = 'finance_diamond_stream';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //添加代理钻石转入记录
    public function createAgentDLog($data)
    {
        $data['auto_grant_at'] = time() + 3600;
        $data['agentd_type'] = 1;
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());
        return DB::table($this->table)->insertGetId($data);
    }

    //更新代理钻石转入记录
    public function updateAgentDLog($id, $data=[], $where=[])
    {
        $upData = [];
        if(isset($data['agentd_type'])) {
            $upData['agentd_type'] = $data['agentd_type'];
        }
        if(!empty($upData)) {
            DB::table($this->table)
                ->where('id', $id)
                ->update($upData);
        }

        return 1;
    }

    //获取用户代理钻石交易记录
    public function getAgentjStreamList($where, $page, $limit)
    {
        $query = DB::table($this->diamondStreamTable)->orderBy('change_timestamp', 'desc')
            ->offset($page * $limit)
            ->limit($limit);
        if(!empty($where['user_id'])) {
            $query = $query->where('user_id', $where['user_id']);
        }
        if(!empty($where['from_user_id'])) {
            $query = $query->where('from_user_id', $where['from_user_id']);
        }
        if(!empty($where['diamond_type'])) {
            $query = $query->whereIn('diamond_type', $where['diamond_type']);
        }
        if(!empty($where['time_st'])) {
            $query = $query->where('change_timestamp', '>=', $where['time_st']);
        }
        if(!empty($where['time_ed'])) {
            $query = $query->where('change_timestamp', '<=', $where['time_ed']);
        }

        $data = $query->get();
        $data = self::objToArray($data);

        return $data;
    }

    /**
     * 获取代理金豆转入记录
     */
    public function getAgentDLog($id){
        $info = DB::table($this->table)
            ->where("id", $id)
            ->first();
        $info = self::objToArray($info);

        return $info;
    }

    /**
     * 获取需要自动发放未领取的代理金豆转入记录
     */
    public function getAgentJdLogByType($time){
        $list = DB::table($this->table)
            ->where("agentd_type", 1)
            ->where("auto_grant_at", '<=', $time)
            ->get();
        $list = self::objToArray($list);

        return $list;
    }



}
